package pixeleditor.gui;

import java.awt.Dimension;
import java.util.ResourceBundle;

import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

import common.gui.IViewable;
import common.i18n.ChangeLanguage;
import common.i18n.IChangeLanguage;

import pixeleditor.ctrl.ControlFactory;

@SuppressWarnings("serial")
public class ChoosePlayerPanel extends JPanel implements IChangeLanguage, IViewable {

	private JRadioButton player1  = new JRadioButton();
	private JRadioButton player2 = new JRadioButton();
	private ButtonGroup group = new ButtonGroup();

	public ChoosePlayerPanel() {
		ChangeLanguage.addObserver(this);
		initComponents();
		initControls();
	}

	private void initComponents() {
		setPreferredSize(new Dimension(70,70));
		setLanguage(ChangeLanguage.currentLanguage);
		group.add(player1);
		group.add(player2);
		//TODO setText for player buttons is not working
		player1.setText("1");
		player2.setText("2");
		add(player1);
		add(player2);
		
	}

	private void initControls() {
		Action a = (Action) ControlFactory.getControl(ControlFactory.CHOOSE_PLAYER_CTRL);
		ControlFactory.getControl(ControlFactory.GRID_CTRL).addView(this);
		player1.setAction(a);
		player2.setAction(a);
		a.putValue("ChoosePlayerPanel.player1", player1);
		a.putValue("ChoosePlayerPanel.player2", player2);
	}

	@Override
	public void setLanguage(ResourceBundle rb) {
		setBorder(new TitledBorder(rb.getString("editorPlayer")));	
	}

	@Override
	public void updateView(Object o) {
		//Unmark the radio buttons if a player has been set
		if(o != null && o.equals("player"))
			group.clearSelection();
		
	}
}
